<?php namespace sacvoyage\storage;


class View {

	public function __construct($gateway, $expression) {
		$this->_gateway=$gateway;
		$this->_expression=$expression;
		if(is_safe_name($expression)) {
			$this->_alias=$expression;
		} else {
			$this->_alias='alias_'.random_string();
		}
	}

	public function getGateway() {
		return $this->_gateway;
	}

	/*
	 * sql-expression to retreive content; 
	 * can be simple table name or somplex sql-expression
	 */
	public function getExpression() {
		return $this->_expression;
	}

	/*
	 * alias of table
	 */
	public function getAlias() {
		return $this->_alias;
	}

	/*
	 * make query on this table
	 */
	public function query($sql, $errorMessage=null) {
		if($errorMessage===null) {
			$errorMessage="error executing query on {$this->_expression}";
		}
		return $this->_gateway->query($sql, $errorMessage);
	}

	/*
	 * escape string using this gateway' parameters
	 */
	public function escape($string) {	
		return $this->_gateway->escape($string);
	}

	private $_gateway;
	private $_expression;
	private $_alias;

}

?>
